package cn.codesea.housework_user.ui.manager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.HomeRcvAdapter;
import cn.codesea.housework_user.adapter.SerListRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickActivityListener;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickServiceListener;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import cn.codesea.housework_user.net.service.SerService;
import cn.codesea.housework_user.ui.AcActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Tab2ServiceManagerFragment extends Fragment{

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    List<Service> sers = new ArrayList<>();
    private SerListRcvAdapter adapter;
    private LinearLayoutManager mLinearLayoutManager;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2_manager_service,container,false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init(){
        initData();
        initView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            initData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView(){

        adapter = new SerListRcvAdapter(getActivity(),sers);
        adapter.setRecyclerItemClickListener(new OnRecyclerItemClickServiceListener() {
            @Override
            public void onItemClick(int Position, List<Service> list) {
                startActivity(new Intent(getActivity(),SerEditActivity.class).putExtra("ser",sers.get(Position)));
            }
        });
        mLinearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);

        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(adapter);

    }

    private void initData() {

        SerService serService = ServiceGenerator.createService(SerService.class);
        Call<Result> call = serService.getChildService();
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });

    }

    private void resultProcess(Result result){
        Type type = new TypeToken<List<Service>>(){}.getType();
        sers.clear();
        sers.addAll((List<Service>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        adapter.notifyDataSetChanged();
    }

}
